package com.nulgame.bukkit.plugin.task;

import java.util.Map;
import java.util.Set;

import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import com.nulgame.bukkit.plugin.AntiSpamPlugin;
import com.nulgame.bukkit.plugin.cleaner.PlayerChatCleaner;
import com.nulgame.bukkit.plugin.logger.ASLogger;
import com.nulgame.bukkit.plugin.model.ChatInfo;

public class PlayerChatCleanerTask extends BukkitRunnable {
	private AntiSpamPlugin m_plugin;
	public PlayerChatCleanerTask(AntiSpamPlugin plugin){
		m_plugin = plugin;
	}
	@Override
	public void run() {
		long begin = System.currentTimeMillis();
		ASLogger.info("Chat cleaner task begins running");
		Set<Player> cleanedPlayer = m_plugin.getCleanedPlayerCandidateCritical();
		Long limitedTimeMillis = m_plugin.getAntiChatSpamLimitedTimeMillis();
		Map<Player, Map<String, ChatInfo>> playerChatMap = m_plugin.getRecentPlayerChatMap();
		if(cleanedPlayer.size() > 0){
			new PlayerChatCleaner(cleanedPlayer, playerChatMap, limitedTimeMillis).clean();
		}else{
			ASLogger.info("No chats to be cleaned");
		}
		ASLogger.info("Chat cleaner task end, took " + (System.currentTimeMillis() - begin) + "ms");
	}

}
